package com.zhiyou100.crm.servlet.message;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.Message;
import com.zhiyou100.crm.pojo.Pager;
import com.zhiyou100.crm.pojo.User;
import com.zhiyou100.crm.service.MessageService;
import com.zhiyou100.crm.service.impl.MessageServiceImpl;


/**
 * 草稿箱
 */
@WebServlet("/message/listSave")
public class ListSaveServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	MessageService messageService=new MessageServiceImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		List<Message> messages = messageService.queryMessage();

		if(messages!=null) {
			List<Message> messages1 = new ArrayList<>();
			User user = (User) request.getSession().getAttribute("user");
			for (int i = 0; i < messages.size(); i++) {
				if ((user.getUser_id()) == (messages.get(i).getSender()) && ((messages.get(i).getSend_status()) == 0)) {
					System.out.println(messages.get(i));
					messages1.add(messages.get(i));
				}
			}
			if (messages1 != null) {
				//分页显示
				//初始页码
				int start = 1;
				int pagerNo = 1;
				//页面大小，设置
				int pageSize = 3;
				//获取当前页码
				String currentPage1 = request.getParameter("pageNo");
				if (currentPage1 != null) {
					int currentPage2 = Integer.parseInt(currentPage1);
					pagerNo = currentPage2;
				}
				//总数据数，集合的大小
				int total = messages1.size();
				//总页数
				int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
				Pager pager = new Pager(total, pagerNo, pageSize, pageCount, start);
				List<Message> messages2 = new ArrayList<>();
				for (int i = (pagerNo - 1) * pageSize; i < pagerNo * pageSize; i++) {
					if (i == messages1.size()) {
						break;
					}
					messages2.add(messages1.get(i));
				}
				request.setAttribute("pager", pager);
				request.setAttribute("messages", messages2);
				request.getRequestDispatcher("/WEB-INF/view/message/listSave.jsp").forward(request, response);
			} else {
				response.sendRedirect(request.getContextPath() + "/WEB-INF/view/message/listSave.jsp");
			}
		}else{
			response.sendRedirect(request.getContextPath() + "/WEB-INF/view/message/listSave.jsp");
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String field = request.getParameter("searchField");
		String keyword = request.getParameter("keyword");
//		List<Message> messages = messageService.queryMessage();
		User user = (User) request.getSession().getAttribute("user");
		if(keyword!=null) {
			Message message=new Message(field,keyword,0,user.getUser_id());
//			Message message=new Message("主题",keyword,0,1);
			List<Message> messages2 = messageService.queryBys(message);
			request.setAttribute("messages",messages2);
			request.getRequestDispatcher("/WEB-INF/view/message/listSave.jsp").forward(request,response);
		}else {
//			request.setAttribute("messages",messages );
			doGet(request,response);
		}
	}
}
